package com.newegg.research;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Util
{
	public static void mergeTrainingData(File[] srcDir, File descDir)
			throws IOException
	{
		if (!descDir.exists())
			descDir.mkdir();
		Set<String> readedFile = new HashSet<String>();
		for (File dir : srcDir)
		{
			File[] filelist = dir.listFiles();
			for (File file : filelist)
			{
				String categoryname = java.net.URLDecoder
						.decode(file.getName());
				System.out.println(descDir.getAbsolutePath() + "/"
						+ categoryname);
				File descFile = new File(descDir.getAbsolutePath() + "/"
						+ java.net.URLEncoder.encode(categoryname));
				if (!descFile.exists())
					descFile.createNewFile();
				BufferedReader br = new BufferedReader(new FileReader(file));
				BufferedWriter bw = new BufferedWriter(new FileWriter(descFile));
				String line = null;
				while ((line = br.readLine()) != null)
				{
					bw.append(line + "\n");
				}
				br.close();
				bw.close();
			}
		}
	}

	public static final String[] participle(String s,
			Map<String, String> preProcessDict)
	{
		Iterator<Entry<String, String>> it = preProcessDict.entrySet()
				.iterator();
		while (it != null && it.hasNext())
		{
			Entry e = it.next();
			String key = (String) e.getKey();
			int i = s.indexOf(key);
			if (i > 0)
				s = s.replaceAll(key, (String) e.getValue());
		}
		return participle(s, "");
	}

	public static final String[] participle(String s, String stopWords)
	{
		if (s == null)
			return new String[]
			{};
		else if (s.length() < 2)
			return new String[]
			{ s };

		String[] array = new String[s.length() - 1];
		int len = 0;
		StringBuilder sb = new StringBuilder();
		
		if (stopWords.length() < 2)
			stopWords = "（()） ,，/-－——~～【】〖〗№ 　+＋*x、\\:：;；×℃";
		
		for (int i = 0; i < s.length() - 1; i++)
		{
			char a = s.charAt(i);
			char b = s.charAt(i + 1);
			if (stopWords.indexOf(b) > -1)
			{
				if (sb.length() > 0)
				{
					array[len++] = sb.append(a).toString();
					array[len++] = b+"";
				}
				sb = new StringBuilder();
			} else if (a > 0x3040 && b > 0x3040 && !stopWords.contains(a + ""))
			{
				array[len++] = a + "" + b;
			} else if (a < 0x3040 && b < 0x3040 && !stopWords.contains(a + ""))
			{
				sb.append(a);
			} else if (a < 0x3040 && b > 0x3040)
			{
				if (sb.length() > 0)
					array[len++] = sb.append(a).toString();
				sb = new StringBuilder();
			}

			if (i == s.length() - 2)
			{
				if (b < 0x3040)
				{
					if (sb.length() > 0)
						array[len++] = sb.append(b).toString();
				}
			}
		}
		String[] result = new String[len];
		System.arraycopy(array, 0, result, 0, len);
		return result;
	}

	public static void main(String[] args) throws IOException
	{
		File f1 = new File("d:/Training_NEWEGG/");
		File f2 = new File("d:/Training_IT168/");
		File f3 = new File("d:/Training_ENET/");
		File f4 = new File("d:/Training_ALL/");
		mergeTrainingData(new File[]
		{ f1, f2, f3 }, f4);
	}
}
